package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.MessageActivityAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MessageActivityModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/24.
 * 消息中心
 */

public class MessageActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.listview)
    ListView listview;

    @Bind(R.id.available_layout)//无消息提示
    LinearLayout available_layout;

    Context context;

    List<MessageActivityModel> message;
    MessageActivityAdapter adapter;
    private MyProgressDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        adapter = new MessageActivityAdapter(context);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(clickListener);

    }
    AdapterView.OnItemClickListener clickListener =new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent=new Intent(context,MessageForDetailsActivity.class);
            intent.putExtra("ints",i);
            intent.putExtra("data", (Serializable) message);
            startActivity(intent);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            HttpClient.Messages(new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    System.out.println("stringmd5result"+result);
                    List<RegistrationModel> getnote = JsonTools.getunderlying(result);
                    if (getnote!=null||getnote.size()>0){
                        String status = getnote.get(0).getStatus();
                        String getMsg = getnote.get(0).getMsg();
                        if (status.equals("200")){
                            String content = getnote.get(0).getContent();
                            message = JsonTools.getMessage(content);
                            if (message!=null||message.size()>0){

                                adapter.addItem(message);
                            }
                            available_layout.setVisibility(View.GONE);
                        }else if (status.equals("105")){
                            Intent intent = new Intent(context, LoginActivity.class);
                            startActivity(intent);
                            ToastUtils.show(context, getMsg);
                        }else {
                            available_layout.setVisibility(View.VISIBLE);
                            // ToastUtils.show(context, getMsg);
                        }
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    available_layout.setVisibility(View.VISIBLE);
                    ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    available_layout.setVisibility(View.VISIBLE);
                    ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                }

                @Override
                public void onFinished() {

                }
            });
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }
}
